package com.example.demo.excel2csv.controller;

import com.example.demo.bigfileupload.util.Result;
import com.example.demo.excel2csv.service.IExcel2CsvService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * ******************************************************
 *
 * @author liugh9
 * @version 1.0
 * @classname Excel2CsvController
 * @description
 * @date 2023/11/22 15:34
 * <p>
 * ******************************************************
 */
@Slf4j
@RestController
@RequestMapping("/excel")
@Api(tags = "excel按条件转csv")
public class Excel2CsvController {

    @Autowired
    private IExcel2CsvService excel2CsvService;

    @PostMapping("/transferCsv")
    @ApiOperation("转csv")
    public Result transferCsv(MultipartFile file) throws IOException {
        long millis = System.currentTimeMillis();
        excel2CsvService.excel2Csv(file);
        log.info("excel转csv共耗时：{}毫秒", System.currentTimeMillis() - millis);
        return Result.success();
    }

    @GetMapping("/transferCsvByDir")
    @ApiOperation("将某个目录下的excel转csv")
    public Result transferCsvByDir() throws IOException {
        long millis = System.currentTimeMillis();
        excel2CsvService.excel2Csv();
        log.info("excel转csv共耗时：{}毫秒", System.currentTimeMillis() - millis);
        return Result.success();
    }

    @GetMapping("/transferCsvByDir/column")
    @ApiOperation("将某个目录下的excel转csv，并按列大小拆分")
    public Result transferCsvByDirAndColumn() throws IOException {
        long millis = System.currentTimeMillis();
        excel2CsvService.transferCsvByDirAndColumn();
        log.info("excel转csv共耗时：{}毫秒", System.currentTimeMillis() - millis);
        return Result.success();
    }
}
